package com.ukao.cashregister.pesenter;

import android.content.Context;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.ReceiveProClothingBean;
import com.ukao.cashregister.bean.ReceiveProListBean;
import com.ukao.cashregister.bean.ReceiveStoreBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.view.ArriveShopView;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by ukaodev on 2017/6/26.
 */

public class ArriveShopPesenter extends BasePresenter<ArriveShopView> {


    public ArriveShopPesenter(ArriveShopView view) {
        attachView(view);
    }


    /**
     * 查询信息 出厂批次号
     */
    public void receiveStore(Context mContext, String batchCode, boolean isOrderEnter) {
        mvpView.showLoading();
        Observable mObservable = null;
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        if (isOrderEnter) {
            mHashMap.put("orderNo", batchCode);/* 查询信息 出厂订单信息*/
            mObservable = apiStores.ordersInfo(Constant.createParameter(mHashMap));
        } else {
            mHashMap.put("batchCode", batchCode);
            mObservable = apiStores.receiveStore(Constant.createParameter(mHashMap));
        }

        addSubscription(mObservable,
                new ApiCallback<ReceiveStoreBean>() {
                    @Override
                    public void onSuccess(ReceiveStoreBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.loadReceiveSucceed(model.getData());
                        } else {

                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 出厂衣物列表：
     */
    public void receiveProList(Context mContext, String outId, boolean isOrderEnter) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        Observable mObservable = null;
        if (isOrderEnter) {  /*根据订单查询 */
            mHashMap.put("orderId", outId);
            mHashMap.put("accessToken", SaveParamets.getToken(mContext));
            mObservable = apiStores.orderProList(Constant.createParameter(mHashMap));
        } else {
            mHashMap.put("outId", outId);/*根据批次*/
            mHashMap.put("accessToken", SaveParamets.getToken(mContext));
            mObservable = apiStores.receiveProList(Constant.createParameter(mHashMap));
        }

        addSubscription(mObservable,
                new ApiCallback<ReceiveProListBean>() {

                    @Override
                    public void onSuccess(ReceiveProListBean model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.loadReceiveProListSucceed(model.getData());
                        } else {

                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /***
     * 衣物到店
     * @param mContext
     * @param strClothcode
     */
    public void clothingStore(Context mContext, String strClothcode) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        mHashMap.put("scanCode", strClothcode);
        Observable mObservable = apiStores.confirmClothingArrive(Constant.createParameter(mHashMap));
        addSubscription(mObservable,
                new ApiCallback<ReceiveProClothingBean>() {
                    @Override
                    public void onSuccess(ReceiveProClothingBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.loadClothingArriveSuccessd(model.getData());
                        } else {
                            mvpView.loadFail(model.getMsg());
                        }
                    }
                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }

    /**
     * 确认到店
     *
     * @param from
     * @param isOrderEnter 是否订单进入
     */
    public void confirmStore(Context mContext, final ReceiveProListBean from, boolean isOrderEnter) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        Observable mObservable;
        if (isOrderEnter) {  /*确定接收 */
            mHashMap.put("id", from.getId() + "");
            mHashMap.put("accessToken", SaveParamets.getToken(mContext));
            mObservable = apiStores.confirmStoreOrder(Constant.createParameter(mHashMap));
        } else {
            mHashMap.put("outId", from.getOutId() + "");
            mHashMap.put("id", from.getId() + "");
            mHashMap.put("productId", from.getProductId() + "");
            mHashMap.put("accessToken", SaveParamets.getToken(mContext));
            mObservable = apiStores.confirmStore(Constant.createParameter(mHashMap));
        }

        addSubscription(mObservable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.loadConfirmStoreSucceed("", from);

                        } else {

                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 全部到店
     *
     * @param mContext
     * @param orderId
     * @param isOrderEnter
     */
    public void confirmAllStore(Context mContext, String orderId, boolean isOrderEnter) {

        HashMap mHashMap = new HashMap<String, String>();
        Observable mObservable;
        if (isOrderEnter) {  /*确定接收 */
            mHashMap.put("orderId", orderId);
            mHashMap.put("accessToken", SaveParamets.getToken(mContext));
            mObservable = apiStores.confirmStoreOrder(Constant.createParameter(mHashMap));
        } else {
            mHashMap.put("outId", orderId);
            mHashMap.put("accessToken", SaveParamets.getToken(mContext));
            mObservable = apiStores.confirmStore(Constant.createParameter(mHashMap));
        }

        addSubscription(mObservable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.loadAallStore(model.getMsg());


                        } else {

                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.allStoreFinish();
                    }

                });
    }

    /**
     * 备注
     */
    public void addRemark(Context context, String id, final String remark) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id", id);
        mHashMap.put("remark", remark);/*备注*/
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.remark(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.loadAddRemark(model.getMsg(), remark);
                        } else {
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


}
